<template>
  <div class="list" ref="wrapper">
    <div>
      <div class="area">
        <div class="area__title">当前城市</div>
        <div class="area__button">
          <div class="area__button__wrapper">
            <div
              class="area__button__wrapper__content"
              @click="handleChangeCity(this.currentCity)"
            >
              {{ this.currentCity }}
            </div>
          </div>
        </div>
      </div>
      <div class="area">
        <div class="area__title">热门城市</div>
        <div class="area__button">
          <div class="area__button__wrapper" v-for="item in hot" :key="item.id">
            <div
              class="area__button__wrapper__content"
              @click="handleChangeCity(item.name)"
            >
              {{ item.name }}
            </div>
          </div>
        </div>
      </div>
      <div class="area" v-for="(item, key) in cities" :key="item.id" :ref="key">
        <div class="area__title">{{ key }}</div>
        <div class="area__item">
          <div
            class="area__item__content"
            v-for="innerItem in item"
            :key="innerItem.id"
            @click="handleChangeCity(innerItem.name)"
          >
            {{ innerItem.name }}
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import BScroll from "better-scroll";
import { mapState, mapMutations } from "vuex";
export default {
  name: "CityList",
  props: {
    hot: Array,
    cities: Object,
    letter: String,
  },
  methods: {
    ...mapMutations(["changeCity"]),
    handleChangeCity(city) {
      // 可以使用 mapMutations 来代替
      // this.$store.commit("changeCity", city);
      this.changeCity(city);
      // console.log(city);
      // 使用 router.push() 进行路由跳转
      this.$router.push("/");
    },
  },
  computed: {
    // ...mapState(['city']),
    ...mapState({
      currentCity: "city",
    }),
  },
  // 使用 watch 监听 letter 的变化
  watch: {
    letter() {
      // 判断数据是否传递过来了
      if (this.letter) {
        const alp = this.letter.trim();
        const element = this.$refs[alp][0];
        // 使用 better-scroll 提供的方法实现跳转到相应的节点
        this.scroll.scrollToElement(element);
      }
    },
  },
  mounted() {
    this.scroll = new BScroll(this.$refs.wrapper);
  },
};
</script>

<style lang="scss" scoped>
.list {
  overflow: hidden;
  position: absolute;
  top: 1.5rem;
  bottom: 0;
  left: 0;
  right: 0;
  .area {
    &__title {
      line-height: 0.54rem;
      font-size: 0.26rem;
      text-indent: 0.2rem;
      color: #666;
      background-color: #eee;
    }
    &__button {
      overflow: hidden;
      padding: 0.1rem 0.6rem 0.1rem 0.1rem;
      &__wrapper {
        float: left;
        width: 33.33%;
        &__content {
          margin: 0.1rem;
          padding: 0.1rem 0;
          border: 0.01rem solid #ccc;
          border-radius: 0.06rem;
          text-align: center;
        }
      }
    }
    &__item {
      &__content {
        line-height: 0.76rem;
        padding-left: 0.2rem;
        border-bottom: 0.01rem solid #eee;
      }
    }
  }
}
</style>